package com.zmx.piglearn.utils

import java.lang.RuntimeException
import java.net.HttpURLConnection
import java.net.URL
import java.nio.charset.Charset

object GetData {
    @JvmStatic
    fun getImage(path: String): ByteArray {
        val url = URL(path)
        val conn = url.openConnection() as HttpURLConnection
        conn.connectTimeout = 5000
        conn.requestMethod = "GET"
        if (conn.responseCode != 200) {
            throw RuntimeException("请求失败")
        }
        val input = conn.inputStream
        val bytes = StreamTool.read(input)
        input.close()
        return bytes
    }

    @JvmStatic
    fun getHtml(path: String): String {
        val url = URL(path)
        val conn = url.openConnection() as HttpURLConnection
        conn.connectTimeout = 5000
        conn.requestMethod = "GET"
        if (conn.responseCode != 200) {
            throw RuntimeException("请求失败")
        }
        val input = conn.inputStream
        val bytes = StreamTool.read(input)
        return String(bytes, Charset.forName("UTF-8"))
    }
}